package com.zsc.projectmanagementapi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.projectmanagementapi.commons.ResponseCode;
import com.zsc.projectmanagementapi.commons.ServerResponse;
import com.zsc.projectmanagementapi.dao.EmployeeInformationMapper;
import com.zsc.projectmanagementapi.dao.UserMapper;
import com.zsc.projectmanagementapi.exception.RException;
import com.zsc.projectmanagementapi.pojo.Do.EmployeeInformation;
import com.zsc.projectmanagementapi.pojo.Do.User;
import com.zsc.projectmanagementapi.pojo.RegisterDAO;
import com.zsc.projectmanagementapi.shiro.JWTToken;
import com.zsc.projectmanagementapi.shiro.JWTUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author Bernie_fang
 * @Description:
 * @create 2021/6/30 15:13
 **/
@Service
public class UserServiceImpl {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeInformationMapper informationMapper;

    public ServerResponse login(String username, String password) throws Exception {
        // 判空
        if (StringUtils.isEmpty(username)) {
            throw new RException(ResponseCode.EMPTY_USERNAME.getDesc(), ResponseCode.EMPTY_USERNAME.getCode());
        }
        if (StringUtils.isEmpty(password)) {
            throw new RException(ResponseCode.EMPTY_PASSWORD.getDesc(), ResponseCode.EMPTY_PASSWORD.getCode());
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);

        if (user == null) {
            throw new RException(ResponseCode.EMPTY_ACCOUNT.getDesc(), ResponseCode.EMPTY_ACCOUNT.getCode());
        }

        if (!user.getPassword().equals(password)) {
            // 密码错误
            throw new RException(ResponseCode.ERROR_PASSWORD.getDesc(), ResponseCode.ERROR_PASSWORD.getCode());
        }
        String token = new String();
        try {
            //生成签名
            token = JWTUtil.signUser(user);
            //构造JWT Token
            JWTToken jwtToken = new JWTToken(token);
            //shiro安全框架：获取subject对象
            Subject subject = SecurityUtils.getSubject();
            //设置token
            subject.login(jwtToken);
        } catch (Exception e) {
            //发生异常，说明登录失败
            return ServerResponse.createLoginFail("登录出错，请检查用户名、密码");
        }

        return ServerResponse.createBySuccess(user, token);
    }

    /**
     * @param register:
     * @Description: 用户注册
     * @Author: Bernie_fang
     * @Since: 2021/6/19 9:36
     * @return: com.college.collegesystem.common.ServerResponse
     **/
    public ServerResponse register(RegisterDAO register) throws RException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", register.getUsername());
        User user = userMapper.selectOne(wrapper);
        if (user != null) {
            return ServerResponse.createBySuccess(400,"该用户名已经被使用，请更换用户名",null);
        }
        user = new User();
        user.setUsername(register.getUsername());
        user.setPassword(register.getPassword());
        user.setValid(1);
        user.setCreatedTime(new Date());
        user.setModifiedTime(new Date());
        user.setDeleted(0);
        int insertSysUser = userMapper.insert(user);
        if (insertSysUser != 1) {
            throw new RException(ResponseCode.ERROR.getDesc(), ResponseCode.ERROR.getCode());
        }

        EmployeeInformation information = new EmployeeInformation();
        information.setUsername(register.getUsername());
        information.setName(register.getName());
        information.setEmail(register.getMail());
        information.setPhone(register.getPhone());
        information.setGender(Integer.parseInt(register.getGender()));
        information.setAddress(register.getAddress());
        information.setWorkExperience(register.getWorkExperience());
        informationMapper.insert(information);

        return ServerResponse.createBySuccess(200,"恭喜您！成功注册！",null);
    }


}
